/* $RCSFile: RecyclerDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/Recycler/RecyclerDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:59  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Helpers;
using AddonNice.Diagnostics;
using AddonNice.Configuration;

namespace AddonNice.Settings
{
	/// <summary>
	/// DB access for recycler.
	/// </summary>
    public class RecyclerDB : DBDataStore
    {
        /// <summary>
        /// MoveModuleToNewTab assigns the given module to the given tab
        /// </summary>
        public void MoveModuleToNewTab(int TabID, int ModuleID)
        {
            using (SqlCommand MyCommand = new SqlCommand("dbo.adn_MoveModuleToNewTab", myConnection))
            {
                MyCommand.CommandType			= CommandType.StoredProcedure;

                AddModuleIDParameters(MyCommand,ModuleID);

                AddPageIDParameters(MyCommand,TabID);

                try
                {
                    myConnection.Open();
                    MyCommand.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    if (DBTraceSwitch.Sw.TraceError)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]RecyclerDB MoveModuleToNewTab TabID : {0}, ModuleId : {1}, ex : {2}", TabID, ModuleID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (myConnection.State == System.Data.ConnectionState.Open)
                        myConnection.Close();
                }
            }
        }


        public DataTable GetModulesInRecycler(int PortalID, string SortField)
        {
            if (DBTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyclerDB GetModulesInRecycler BEG PortalID : {0}, SortField: {1}", PortalID, SortField),DBTraceSwitch.Sw.Info);
            using (SqlDataAdapter MyAdapt = new SqlDataAdapter("dbo.adn_GetModulesInRecycler", myConnection))
            {
                MyAdapt.SelectCommand.CommandType		= CommandType.StoredProcedure;
                
                AddPortalIDParameters(MyAdapt.SelectCommand,PortalID);

                SqlParameter ParameterSortField			= new SqlParameter("@SortField", SqlDbType.VarChar, 50);
                ParameterSortField.Value				= SortField;
                MyAdapt.SelectCommand.Parameters.Add(ParameterSortField);

                DataTable myDataTable = new DataTable();
                try
                {
                    myConnection.Open();
                    MyAdapt.Fill(myDataTable);
                    if (DBTraceSwitch.Sw.TraceVerbose)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RecyclerDB GetModulesInRecycler Table Count: {0}", myDataTable.Rows.Count),DBTraceSwitch.Sw.Info);
                }
                catch (Exception exp)
                {
                    if (DBTraceSwitch.Sw.TraceError)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]RecyclerDB GetModulesInRecycler PortalID: {0}, ex: {1}", PortalID, exp),DBTraceSwitch.Sw.Info);
                    throw;
                }
                finally
                {
                    if (myConnection.State == System.Data.ConnectionState.Open)
                        myConnection.Close();
                }
                return myDataTable;
            }
        }
    }
}
